package com.hw.one.collect.movingsource.slagcar.dao;

import com.hw.one.collect.movingsource.slagcar.po.PatrolDataPo;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/4.
 */
@Repository(value = "patrolDataDao")
public class PatrolDataDao {

    @Resource
    private MongoTemplate mongoTemplateStandardSiteData;

    private static String tableName = "T_DATA_DEVICE_PATROL_TRACK";


    public void savePatrolData(PatrolDataPo patrolDataPo) {
        mongoTemplateStandardSiteData.insert(patrolDataPo, tableName +"-" + patrolDataPo.getMONITORTIME().substring(0, 4));
    }


    public void savePatrolDataList(List<PatrolDataPo> raiseDust) {
        if (raiseDust.size() > 0) {
            mongoTemplateStandardSiteData.insert(raiseDust, tableName + "-" + raiseDust.get(0).getMONITORTIME().substring(0, 7).replace("-", ""));
        }
    }



    /**
     * 更新实时表数据
     * @autor 李亚坤
     * @createtime 2017年8月14日10:17:42
     * @param raiseDustList
     */
    public void updateRealRaiseDust(List<PatrolDataPo> raiseDustList) {
        List<String> siteCodeList = new ArrayList<>();
        for (PatrolDataPo raiseDust : raiseDustList) {
            siteCodeList.add(raiseDust.getDEVICECODE());
        }
        Criteria criteria = Criteria.where("DEVICECODE").in(siteCodeList);
        Query query = new Query(criteria);
        // 先删除
        mongoTemplateStandardSiteData.remove(query, tableName);
        // 再插入
        mongoTemplateStandardSiteData.insert(raiseDustList, tableName);


    }
}
